package leetcode.code0563;

import leetcode.helper.tree.TreeNode;

public class Solution563 {

	public int findTilt(TreeNode root) {
		if (root == null) {
			return 0;
		}
		return this.find(root)[0];
	}

	private int[] find(TreeNode root) {
		int L0 = 0;
		int L1 = 0;
		if (root.left != null) {
			int[] left = this.find(root.left);
			L0 = left[0];
			L1 = left[1];
		}
		int R0 = 0;
		int R1 = 0;
		if (root.right != null) {
			int[] right = this.find(root.right);
			R0 = right[0];
			R1 = right[1];
		}
		return new int[] { Math.abs(L1 - R1) + L0 + R0, L1 + R1 + root.val };
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
